/**
 * Created by sun on 2016/8/4.
 */
define(function (require) {
    var app = require('/../js/app');

    var datepicker = require('datepicker');
    var toastr =require('toastr');

    require('upload');

    app.useModule("ngFileUpload");

    app.controller('massageCtrl', ['$scope','$http' ,'Upload','$filter',function ($scope, $http,Upload,$filter) {
        datepicker($scope);
        $scope.mobile={};

        $scope.message={};


//获取短信余额
        $scope.getMessageNum= function () {
            $http.post("/sys/smsMessage/querySms",{})
                .success(function (data){
                    console.log(data)
                    $scope.message.num=data.remainFee;
                }).error(function(data){
                });
        };


        $scope.getHours= function () {
            var hours=[];
            for(var i=0;i<24;i++){
                if(i<10){
                    i="0"+i
                }
                hours.push(i)
            }
            return hours;
        };
        $scope.getMins= function () {
            var mins=[];
            for(var i=0;i<60;i++){
                if(i<10){
                    i="0"+i
                }
                mins.push(i)
            }
            return mins;
        };

//上传指定用户
        $scope.upload = function (file) {
            Upload.upload({
                url: "/sys/smsMessage/listUploadUser",
                data: {file: file}
            }).then(function (resp) {

                    $scope.mobile.mobileString = resp.data.mobile;

            }, function (resp) {
                console.log(resp)

            }, function (evt) {

                    var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
                    console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);

            });
        };


        //字数统计
        $scope.checkText=function(){

            $scope.mobile.wordsize=$scope.mobile.content.length;
            if($scope.mobile.content.length<=70){
                $scope.mobile.sms=1;
                $scope.mobile.isTrue=false;

            }
            else if($scope.mobile.content.length>70 && $scope.mobile.content.length<=140) {
                $scope.mobile.sms=2;
                $scope.mobile.isTrue=false;
            } else if($scope.mobile.content.length>140 && $scope.mobile.content.length<=210) {
                $scope.mobile.sms=3;
                $scope.mobile.isTrue=false;
            }else if($scope.mobile.content.length>210 && $scope.mobile.content.length<=280) {
                $scope.mobile.sms=4;
                $scope.mobile.isTrue=false;
            }else if($scope.mobile.content.length>280 && $scope.mobile.content.length<=350) {
                $scope.mobile.sms=5;
                $scope.mobile.isTrue=false;
            }else if($scope.mobile.content.length>350 && $scope.mobile.content.length<=420) {
                $scope.mobile.isTrue=false;
                $scope.mobile.sms=6;
            }else if($scope.mobile.content.length>420 && $scope.mobile.content.length<=490) {
                $scope.mobile.isTrue=false;
                $scope.mobile.sms=7;
            }else if($scope.mobile.content.length>490) {
                $scope.mobile.isTrue=true;
                $scope.mobile.sms=7;
            }

        };

        //
        ////计数用户
        //$scope.checkUser= function () {
        //    var str =$scope.mobile.mobileString;
        //
        //    var reg= /^\d{11}(,\d{11})*$/;
        //
        //    if(reg.test(str)) {
        //
        //        $scope.mobile.isTrue = false;
        //
        //    }
        //    else{
        //            $scope.mobile.isTrue=true
        //        }
        //    var count = str.match(/,/g).length+1;
        //    $scope.mobile.user=count
        //
        //};
  //发送短信

        $scope.sendSubmit= function (isValid) {
            if (!isValid) {
                toastr.error("信息填写有误");
            }

            var param={
                mobileString:$scope.mobile.mobileString,
                content:$scope.mobile.content,
                sendDateTime:null,
                title:'短信群发'
            };

            if($scope.message.time=='0'){
                var fistdate=$filter('date')($scope.message.yearx,'yyyy-MM-dd');
                var firsthour=$scope.message.hour;
                var firstmin=$scope.message.min;
                param.sendDateTime= fistdate+' '+firsthour+':'+firstmin+':'+'00';
            }



            console.log(param);

            $http.post("/sys/smsMessage/send",param)
                .success(function (data){

                    if(data.result='success'){
                        toastr.success('发送成功');
                        $scope.mobile.mobileString='';
                        $scope.mobile.content='';
                        $scope.mobile.wordsize='';
                        $scope.mobile.sms='';
                        $scope.message.yearx='';
                        $scope.message.hour='';
                        $scope.message.min='';
                        $scope.message.time='';
                        $scope.mobile.isTrue=false;
                        $scope.getMessageNum();
                    }

                }).error(function(data){
                    toastr.success('发送失败');
                });
        };



    }]);


});